package com.atguigu.spzx.cloud.order.config;

import feign.RetryableException;
import feign.Retryer;

/**
 * projectName: com.atguigu.spzx.cloud.order.config
 *
 * @author: 赵伟风
 * description:
 */
public class MyRetryer implements Retryer {


    private int counter = 0 ;
    private int end = 2;


    //todo: 重试两次,不行抛异常!
    //重要方法,每次重试都会调用此方法!再次决定 [1.重试的次数  2. 间隔(默认间隔[100]毫秒=0.1秒 1秒) 3.达到次数以后如何处理]
    @Override
    public void continueOrPropagate(RetryableException e) {
        if (counter >= end){  //2次
            //超出了重试次数
            throw new RuntimeException("重试超出2次!!");
            //throw new RuntimeException(e);
            //throw e;
        }
        counter++;
        System.err.println("第"+counter+"次重试!!");
        try {
            Thread.sleep(900);
        } catch (InterruptedException ex) {
            throw new RuntimeException(ex);
        }
    }

    //返回当前对象 this => 一个对象 || new 每次都是新的对象
    @Override
    public Retryer clone() {
        return new MyRetryer();
    }
}
